<?php
// +----------------------------------------------------------------------
// | 杭州十禾信息科技有限公司
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.shelpe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LeePuman <ysyzlsm@163.com>
// +----------------------------------------------------------------------

class IndexAction extends NewzanAction
{
	private $is_admin;

	public function _initialize()
    {
        parent::_initialize();
		$is_admin = session('admin');
		$this->is_admin = $is_admin ? true : false;
		unset($is_admin);
    }

	public function index()
	{
		$auth_user_id = $this->_request('auth_user');
		// 开放管理员快速登录
		if($auth_user_id && $this->is_admin) {
			
			$whr = array('user_id' => $auth_user_id);
			$authUser = D('User')->where($whr)->find();
			if(!$authUser) {
				$this->error('给定的用户不存在');
			} else {
				// 获取当前账户的主账户信息
				$current_manager = D('User')->where('user_id = ' . $authUser['authorized_to'])->find();
				$oauth_user_id = ($authUser['item_code'] && $authUser['type']) ? $authUser['user_id'] : $authUser['authorized_to'];
				$oauth = D('oauth')->where('taobao_user_id = ' . $oauth_user_id)->find();
				if(empty($oauth))
					throw new NewzanException('找不到该用户oauth授权', RetStatus::LOGIC_ERR);
				$authUser['oauth'] = $oauth;
				$current_manager['oauth'] = $oauth;

				//切换用户session，保存领导的session到manger
				$this->me = $authUser;
				$this->manager = $current_manager;
				session('me', $authUser);
				session('manager', $current_manager);

				require_once LIB_PATH . "Library/TaobaoAPI/Curl.php";
        		R('Home/User/getAccountToken', array('user_id'=>$authUser['user_id'], 'http' => new Curl()));
        		$this->userLoginrecord('admin');
			}
		}
		$user_id = $this->me["user_id"];

		$this->loadJS('date', 'daterangepicker', 'component/component.campaign.reports', 'home/home', 'home/welcome');
		$campaignMdl = D("Campaign");
		$this->campaigns = $campaignMdl->getCampaigns($user_id, false);
		$this->from = date("Y-m-d" , strtotime("-7 day"));
		$this->to =  date("Y-m-d" , strtotime("-1 day"));
		$this->assign('me' , $this->me);
		$this->assign('manager' , $this->manager);
		$this->display();
	}

    public function notfound()
    {
    	$this->display();
    }
    
    /** 产品环境下错误页面 */

    public function error($msg, $code = 500, $ajax = false)
    {
    	$error_msg = $this->_get('msg');
    	if(!$error_msg)
    		$error_msg = $msg;
    	throw new NewzanException($msg, RetStatus::INNER_ERR);
    }


	/*判断电话邮箱qq是否存在*/
	public function checktel(){
		$tel = I('get.tel');
		$user_info = M('user_info');
		$result = $user_info->where(array('tel'=>$tel))->find();
		if($result){
			echo 1;
		}else{
			echo 0;
		}
	}

	public function checkqq(){
			$qq = I('get.qq');
			$user_info = M('user_info');
			$result = $user_info->where(array('qq'=>$qq))->find();
			if($result){
				echo 1;
			}else{
				echo 0;
			}
		}

		public function checkmail(){
			$mail = I('get.mail');
			$user_info = M('user_info');
			$result = $user_info->where(array('email'=>$mail))->find();
			if($result){
				echo 1;
			}else{
				echo 0;
			}
		}

	public function userdetail(){
		$user_id = $this->me['user_id'];
		$user_info = M('user_info');
		$data['tel'] = $_POST['tel'];
		$data['qq'] = $_POST['qq'];
		if($_POST['mail']){
			$data['email'] = $_POST['mail'];
		}

		$where['user_id'] = $user_id;
		if($user_info->where($where)->select()){
			$test = $user_info->where($where)->save($data);
		}else{
			$data['user_id'] = $user_id;
			$test = $user_info->data($data)->add();
		}

		$_SESSION['me']['status'] = 1;
		if($test){
			$user = M('users');
			$user->where($where)->save(array('status' => 1));
			$this->display_json(array('msg'=>'添加成功!'));
		}else{
			$this->display_json(array('msg'=>'添加失败!'), RetStatus::INTER_ERR);
		}
	}

	public function userFirst(){
		$user_id = $this->me['user_id'];
		$user = M('users');
		$where['user_id'] = $user_id;
		$data['userfirst'] = 1;
	
		$user->where($where)-> save($data);	
	}

	public function getUserfirst(){
		$user_id = $this->me['user_id'];
		$user = M('users');
	    $where['user_id'] =$user_id;
		$where['userfirst'] =1;
		$result = $user->where($where)->find();
		if($result){
			$this->display_json(array('msg'=>'查询成功!','userfirst'=>$result['userfirst']));
		}else{
			$this->display_json(array('userfirst'=>0));
		}	
		
	}

		/*用户登录信息记录*/
	public function userLoginrecord($source){
		$data['user_id'] = $this->me['user_id'];
		$record = M('userLoginRecords');
		// $where['user_id'] = $user_id;
		$data['nick'] = $this->me['nick'];
		$data['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
		$data['login_ip'] = ip2long($_SERVER['REMOTE_ADDR']);
		$data['login_time'] = date('Y-m-d H:i:s');
		// if($_GET['login_mode'] == "code"){
		// 	$data['login_mode'] = "code";
		// }else if($_GET['login_mode'] == "bos"){
		// 	$data['login_mode'] = "bos";
		// }else if($_GET['login_mode'] == "admin"){
		// 	$data['login_mode'] = "admin";
		// }
		$data['login_mode'] = $source;
		$result = $record->add($data);
	}

	public function notice()
	{
		$code = RetStatus::OK;
		try {
			$res = M('notice')->
					   field('content')->
					   where(array(
					   			'end_time' => array('GT', date('Y-m-d H:i:s', time()))
							))->
				   select();

			$result = array(
				'total_item' => 0,
			);
			
			if($res) {
				$res = array_column($res, 'content');
				$result['total_item'] = count($res);
				$result['items'] = $res;
			}
		} catch (Exception $e) {
			$result['msg'] = $e->getMessage();
			$code = RetStatus::INNER_ERR; 
		}
		
		$this->display_json($result, $code);
	}
}